<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="searchInpt"
        placeholder="输入ID/昵称/手机号"
        clearable
        @keyup.enter.native="search()"
        class="search-btn"
      >
        <el-button
          type="primary"
          @click="search()"
          icon="el-icon-search"
          slot="append"
          :loading="searchLoading"
        >搜索</el-button>
      </el-input>
      <div class="lz-tips">
        <label>用户筛选:</label>
      </div>
      <el-select
        v-model="userStatusValue"
        placeholder="用户筛选"
        @change="search()"
      >
        <el-option
          v-for="item in userStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <div class="lz-tips">
        <label>活动筛选:</label>
      </div>
      <el-select
        v-model="ultrasonicStatusValue"
        placeholder="活动筛选"
        @change="search()"
      >
        <el-option
          v-for="item in ultrasonicStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <div class="table-container">
      <Table :table="dataTable">
        <template
          slot-scope="props"
          slot="upload"
        >
          <el-button
            type="text"
            @click="uploadImgDialogBox(props.obj.row)"
            size="small"
          >上传阴超</el-button>
        </template>
        <template
          slot-scope="props"
          slot="handle"
        >
          <el-button
            type="text"
            @click="openUltrasoundMessage(props.obj.row)"
            size="small"
          >{{props.obj.row.joinStatus=='0'?'参加':'取消'}}</el-button>

        </template>
      </Table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          v-if="pageshow"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      title="上传阴超"
      :visible.sync="showUploadImgDialog"
      width="30%"
    >
      <div>
        <el-input
          v-model="uploadImgData.name"
          placeholder="请输入姓名"
          style="width:200px;margin-bottom:10px;"
        ></el-input>
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :on-remove="handleRemove"
          name="files"
          :on-success="handleAvatarSuccess"
          :headers="imgHeaders"
          multiple
          :file-list="fileList"
          list-type="picture"
        >
          <el-button
            size="small"
            type="primary"
          >选择图片</el-button>
        </el-upload>
        <div style="margin:10px 0;">
          <el-radio-group v-model="uploadImgData.ovuolaStatus">
            <el-radio :label="0">未排卵</el-radio>
            <el-radio :label="1">已排卵</el-radio>
          </el-radio-group>
        </div>
        <div style="margin-top:10px;">
          <el-date-picker
            v-model="uploadImgData.time"
            type="datetime"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <div style="margin-top:10px;">
          <el-button
            size="small"
            type="primary"
            @click="uploadImgUltrasound()"
          >提交</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import Table from "@/components/Table";
import {
  fetchUltrasoundPlan,
  fetchUltrasoundPlanStatus,
  fetchUltrasound
} from "@/api/table";
import { formatDateTime } from "@/utils/common";
import { getToken } from "@/utils/auth";
export default {
  data() {
    return {
      //总页码
      count: 0,
      //单页行数
      limit: 20,
      //页码
      currentPage: 1,
      searchInpt: "",
      searchLoading: false,
      //删选用户类型
      userStatusValue: "",
      userStatus: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "正式用户"
        },
        {
          value: "1",
          label: "测试用户"
        }
      ],
      //B超计划类型
      ultrasonicStatusValue: "1",
      ultrasonicStatus: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "不参与"
        },
        {
          value: "1",
          label: "参与"
        }
      ],
      dataTable: {
        loading: false,
        border: true,
        hasOperation: false,
        tr: [
          {
            id: "1",
            label: "ID",
            prop: "userId",
            show: true
          },
          {
            id: "2",
            label: "手机号",
            prop: "userName",
            show: true
          },
          {
            id: "3",
            label: "昵称",
            prop: "nickName",
            show: true
          },
          {
            id: "4",
            label: "绑定设备",
            prop: "hardwareEquipment",
            show: true
          },
          {
            id: "5",
            label: "B超计划",
            prop: "joinStatusValue",
            show: true
          },
          {
            id: "6",
            label: "阴超次数",
            prop: "count",
            show: true
          },
          {
            id: "7",
            label: "阴超",
            prop: "upload",
            show: "template"
          },
          {
            id: "8",
            label: "操作",
            prop: "handle",
            show: "template"
          }
        ],
        data: [],
        operation: {}
      },
      //上传图片
      uploadUrl: "",
      imgHeaders: {
        Authorization: ""
      },
      fileList: [],
      showUploadImgDialog: false,
      uploadImgData: {
        time: "",
        imgList: "",
        ovuolaStatus: 0,
        name: ""
      },
      pageshow: true
    };
  },
  components: {
    Table
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.getUltrasoundPlan();
      this.imgHeaders.Authorization = getToken();
      //上传图片链接
      this.uploadUrl = process.env.BASE_API + "/picture/upload";
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getUltrasoundPlan();
    },
    search() {
      this.currentPage = 1;
      this.pageshow = false;
      this.getUltrasoundPlan();
      this.$nextTick(() => {
        this.pageshow = true;
      });
    },
    uploadImgDialogBox(row) {
      this.fileList = [];
      this.uploadImgData.imgList = [];
      this.showUploadImgDialog = true;
      this.uploadImgData.userId = row.userId;
    },
    handleRemove(file, fileList) {
      this.uploadImgData.imgList = fileList;
    },
    handleAvatarSuccess(res, file, fileList) {
      if (res.status === 200) {
        this.uploadImgData.imgList = fileList;
        this.$message.success("上传成功！");
      } else {
        this.$message.error(res.msg);
      }
    },
    openUltrasoundMessage(row) {
      this.$confirm("确认修改", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.changeUltrasoundPlanStatus(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改"
          });
        });
    },
    async uploadImgUltrasound() {
      if (this.uploadImgData.imgList) {
        let imglist = [];
        this.uploadImgData.imgList.forEach(item => {
          imglist.push(item.response.data[0]);
        });
        await fetchUltrasound({
          userId: this.uploadImgData.userId,
          ultrasoundTime: formatDateTime(this.uploadImgData.time),
          ovuolaImage: imglist.toString(),
          ovuolaStatus: this.uploadImgData.ovuolaStatus,
          name: this.uploadImgData.name
        });
        this.showUploadImgDialog = false;
        this.getUltrasoundPlan();
        this.$message({
          type: "success",
          message: "提交成功"
        });
      } else {
        this.$message({
          type: "error",
          message: "请选择图片"
        });
      }
    },
    async changeUltrasoundPlanStatus(row) {
      const rowData = {};
      rowData.userId = row.userId;
      row.joinStatus == "1"
        ? (rowData.planStatus = "0")
        : (rowData.planStatus = "1");
      await fetchUltrasoundPlanStatus(rowData);
      this.$message({
        type: "success",
        message: "修改成功!"
      });
      this.getUltrasoundPlan();
    },
    async getUltrasoundPlan() {
      this.dataTable.loading = true;
      this.searchLoading = true;
      const res = await fetchUltrasoundPlan({
        pageSize: this.limit,
        pageNo: this.currentPage,
        key: this.searchInpt,
        status: this.userStatusValue,
        planStatus: this.ultrasonicStatusValue
      });
      this.dataTable.data = [];
      this.count = res.data.total;
      this.dataTable.loading = false;
      this.searchLoading = false;
      res.data.list.forEach(item => {
        const data = {};
        data.userId = item.userId;
        data.nickName = item.nickName;
        data.userName = item.userName;
        data.hardwareEquipment = item.hardwareEquipment;
        data.count = item.count;
        data.joinStatus = item.joinStatus;
        if (item.joinStatus == "0") {
          data.joinStatusValue = "未参加";
        } else {
          data.joinStatusValue = "已参加";
        }
        this.dataTable.data.push(data);
      });
    }
  }
};
</script>
